<template>
  <div>
    <el-tree
      ref="tree"
      :data="permissionList"
      show-checkbox
      default-expand-all

      :check-strictly="true"

      node-key="id"

      :props="{label :'name'}"
    />
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button size="small">取消</el-button>
        <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { getPermissionList } from '@/api/permission'
import { addSonDepartments } from '@/utils'
import { assignPremission, getRolesDetail } from '@/api/settings'
export default {
  name: 'VueHrThirdAssignPermission',
  props: {
    perId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      permissionList: []
    }
  },
  created() {
    this.loadPermissionList()
  },
  methods: {
    async loadPermissionList() {
      const res = await getPermissionList()
      const detail = await getRolesDetail(this.perId)

      console.log(res, 'getPermissionList')
      console.log(detail, 'getRolesDetail')
      this.permissionList = addSonDepartments(res.data)
      this.$refs.tree.setCheckedKeys(detail.data.permIds)
    },
    async hSubmit() {
      const permIds = this.$refs.tree.getCheckedKeys()
      const res = await assignPremission({ id: this.perId, permIds })
      console.log(res)

      this.$emit('success')
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
